<!--
 * @description  : 证件-卷内备考表
 * @descriptionDetail: 证件-卷内备考表
 * @copyright    : 浙江烟草
 * @author       : sy
 * @create       : 2022-07-17 15:29:13
-->
<template>
  <view class="page">
    <!-- #ifdef H5-ONLY -->
    <theme-navbar :isWhite="false" backText="返回" title="卷内备考表" style="z-index: 99999">
    </theme-navbar>
    <!-- #endif -->
    <view class="form_content">
      <u-form :model="form" label-width="auto" :error-type="errorType" :label-style="lab_style" ref="uForm">
        <u-form-item label="本卷情况说明:" prop="fileDesc" label-position="top">
          <u-input placeholder="" :disabled="isDisabled" type="textarea" v-model="form.fileDesc" />
        </u-form-item>
        <u-form-item label="立卷人:" prop="regPerson" label-position="left">
          <u-input placeholder="" :disabled="isDisabled" input-align="right" v-model="form.regPerson" />
        </u-form-item>
        <u-form-item label="立卷时间:" prop="regDate" :label-width="180" right-icon="arrow-right">
          <u-input
            placeholder=""
            input-align="right"
            :disabled="disable"
            v-model="form.regDate"
            @tap="clickShow"
          />
        </u-form-item>
      </u-form>
    </view>
    <view class="bottom-btns">
      <u-row>
        <u-col span="6" style="text-align: center;">
          <u-button
            plain
            type="primary"
            :loading="submitBtnLoading"
            v-if="!docIns.step || docIns.step == 'edit'"
            @click="handlerDoSubmit(0)"
          >
            保存
          </u-button>
        </u-col>
        <u-col span="6" style="text-align: center;">
          <u-button
            type="primary"
            :loading="submitBtnLoading"
            v-if="!docIns.step || docIns.step == 'edit'"
            @click="handlerDoSubmit(1)"
          >
            提交
          </u-button>
        </u-col>
      </u-row>
    </view>
    <u-calendar :mode="mode" max-date="9999-12-31" v-model="dateShow" @change="calendarChange"></u-calendar>
  </view>
</template>

<script>
import certificateService from "@/service/certificate/certificate.service.js";
import {
  dateUtils,
} from "@/common/util";
import commonService from "@/service/common/common.service";
import {
  LOG_STATUS,LOG_OP_TYPE,
} from '@/service/common/mapping/enum.js';
export default {
  data() {
    return {
      form: {
        fileDesc: "",
        regPerson: "",
        regDate: "",
        auditUnits: "",
        fileRefUuid: "",
        handleMainUuid: "",
      },
      docIns: {},
      errorType: ['border-bottom','message',],
      lab_style: {
        fontWeight: 500,
        color: this.$theme.u_main_color,
        fontSize: '.75rem',
      },
      beginTime: "",
      disable: true,
      data: "",
      mode: "date",
      isDisabled: false,
      dateShow: false, // 日期开关
      submitBtnLoading: false, // 提交按钮显示加载
      dossierObj: {},
      preParams: {},
    }
  },
  created() {
    this.init()
  },
  onLoad() {
    let obj = this.getRouteParam();
    console.log(obj)
    if (obj) this.objMerge(this.preParams, obj)
  },
  mounted() {
    this.initData()
  },
  methods: {
    calendarChange(d) {
      this.form.regDate = dateUtils.dateFormat("yyyy年MM月dd日", new Date(d.result));
      // console.log(d)
    },
    clickShow() {
      if (this.isDisabled) return;
      this.dateShow = true
    },
    init() {
      certificateService.getDocIns(this.preParams.docInsUuid).then(dr => {
        if (dr.success) {
          this.docIns = dr.bean;
          this.isDisabled = this.docIns.step != 'edit';
        } else {
          this.$u.toast(dr.msg);
        }
      }).catch(err => {
        this.$u.toast(err);
      })
    },
    initData() {
      commonService.getCurrentTime().then(res2 => {
        this.beginTime = res2.success ? res2.msg : this.$u.timeFormat(Date.now(),
          'yyyy-mm-dd hh:MM:ss');
      });
      certificateService.preparForm({
        id: this.preParams.docInsUuid,
        docUuid: this.preParams.docUuid,
        bizUuid: this.preParams.bizUuid,
      }).then(res => {
        if (res.success) {
          console.log(res)
          if (res.data.length == 1) {
            this.dossierObj = res.data[0];
          } else {
            this.dossierObj = Object.assign(res.data[0], res.data[1]);
          }
          this.form = this.dossierObj
        }
      }).catch(err => {
        console.log(err)
      })
    },
    saveDocExam() {
      let param = this.form
      certificateService.saveDocExam(param).then(dr => {
        this.$u.hideLoading();
        if (dr.success) {
          this.saveBehaviorLog(0, this.docIns);
          this.$u.toast("保存成功");
          this.triggerPrevRefresh();
        } else {
          this.$u.toast(dr.msg);
        }
      }).catch(err => {
        this.$u.toast(err);
      })
    },
    submitDocExam() {
      let param = this.form
      certificateService.submitDocExam(param).then(dr => {
        this.$u.hideLoading();
        if (dr.success) {
          this.$u.toast("提交成功");
          this.init();
          this.saveBehaviorLog(1, this.docIns);
          this.backPrev({
            paramKey: "examPreRefresh",
            param: true,
          });
        } else {
          this.$u.toast(dr.msg);
        }
      }).catch(err => {
        this.$u.toast(err);
      })
    },
    handlerDoSubmit(status) {
      this.$modal.confirm(`确定${status == 1 ? '提交' : '保存'}吗？`, () => {
        this.$u.loading(`${status == 1 ? '提交' : '保存'}中...`)
        let param = this.form
        if (status == 0) {
          this.saveDocExam()
        } else {
          certificateService.saveDocExam(param).then(dr => {
            this.$u.hideLoading();
            if (dr.success) {
              this.$u.toast("保存成功");
              this.triggerPrevRefresh();
              this.submitDocExam()
            } else {
              this.$u.toast(dr.msg);
            }
          }).catch(err => {
            this.$u.toast(err);
          })
        }
      })
    },
    /**
			 * @param {Boolean} status 提交还是保存
			 */
    saveBehaviorLog(status, doc) {
      commonService.getCurrentTime().then(res2 => {
        let param = {
          bizUuid: doc.bizUuid,
          docUuid: doc.docUuid,
          docInsUuid: doc.docInsUuid,
          bizType: LOG_STATUS.B007,
          operationType: status == 0 ? LOG_OP_TYPE.saveDoc : LOG_OP_TYPE.submitDoc,
          beginTime: this.beginTime,
          endTime: res2.success ? res2.msg : this.$u.timeFormat(Date.now(),
            'yyyy-mm-dd hh:MM:ss'),
          remark: doc.configIns.docName,
        };
        this.beginTime = param.endTime;
        commonService.saveBehaviorLog(param).then(res => {
          console.log(res);
        }).catch(e => {
          console.log(e);
        });
      });
    },
  },
}
</script>

<style lang="scss" scoped>
	@import "@/style/common/rightMenu.scss";

	.form_content {
		padding: 0 30rpx 144rpx;
	}

	.bottom-btns {
		position: fixed;
		bottom: 0px;
		width: 100%;
		background-color: #f8f8f8;
		padding: 5px 0px;
	}
</style>
